/**
 * @title AttentionMatchListAdapter.java
 * @package com.esun.cheetah.view.personal
 * @description 关注的比赛列表使用的适配器
 * @author liugan
 * @date 2012-5-4
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.personal.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.cheetah.R;

import com.esun.cheetah.biz.personal.attention.CancelAttenMatch;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.model.personal.attention.AttentionGameInfo;
import com.esun.cheetah.model.personal.attention.AttentionGroupInfo.GroupState;

/**
 * @title AttentionMatchListAdapter
 * @description 关注的比赛列表使用的适配器
 * @author liugan
 */
public class AttentionMatchListAdapter extends AbsAttentionAdapter {

	private ArrayList<ArrayList<AttentionGameInfo>> mChildList = new ArrayList<ArrayList<AttentionGameInfo>>();

	/** 标识是否正在执行取消关注 */
	private boolean cancelAttentionIsDoing;

	/** 取消关注项的位置 */
	private int canceledGroupPosition;
	private int canceledChildPosition;

	private Handler mCancelAttentionHandler = new CancelAttentionHandler();

	public AttentionMatchListAdapter(Context context) {
		super(context);

		// 固定的3个group项
		mChildList.add(new ArrayList<AttentionGameInfo>());
		mChildList.add(new ArrayList<AttentionGameInfo>());
		mChildList.add(new ArrayList<AttentionGameInfo>());
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder childViewHolder;
		if (convertView == null) {
			convertView = (View) LayoutInflater.from(context).inflate(
					R.layout.attention_match_list_child_item, null);
			childViewHolder = new ChildViewHolder();
			// 初始化ViewHolder
			childViewHolder.rlMatchContainer = (RelativeLayout) convertView
					.findViewById(R.id.rl_attention_match_list_item_container);
			childViewHolder.tvLeagueName = (TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_league_name);
			childViewHolder.tvHostNumber = (TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_host_team_number);
			childViewHolder.tvHostName = (TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_host_team_name);
			childViewHolder.tvHostScore = (TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_host_team_score);
			childViewHolder.tvGuestNumber = (TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_guest_team_number);
			childViewHolder.tvGuestName = (TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_guest_team_name);
			childViewHolder.tvGuestScore = (TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_guest_team_score);
			childViewHolder.tvTimeState = (TextView) convertView
					.findViewById(R.id.tv_match_list_score_item_match_time_state);
			childViewHolder.ivMark = (ImageButton) convertView
					.findViewById(R.id.ib_match_list_score_item_mark);

			childViewHolder.rlEmptyContainer = (RelativeLayout) convertView
					.findViewById(R.id.rl_attention_match_list_no_record_container);
			childViewHolder.rlProgress = (RelativeLayout) convertView
					.findViewById(R.id.rl_attention_match_list_item_loading_container);
			convertView.setTag(childViewHolder);
		} else {
			childViewHolder = (ChildViewHolder) convertView.getTag();
		}

		final GroupState state = mGroupList.get(groupPosition).getState();

		if (state == GroupState.normal) {// 当有比赛信息可以显示时
			convertView.setBackgroundResource(R.drawable.s_item_background);
			// 设置比赛项比分相关内容
			AttentionGameInfo matchInfo = mChildList.get(groupPosition).get(
					childPosition);
			childViewHolder.tvLeagueName.setText(matchInfo.getLeagueName());
			childViewHolder.tvHostNumber.setText(matchInfo.getHomeNumber());
			childViewHolder.tvHostName.setText(matchInfo.getHome());
			childViewHolder.tvGuestNumber.setText(matchInfo.getAwayNumber());
			childViewHolder.tvGuestName.setText(matchInfo.getAway());

			Resources resources = context.getResources();

			switch (matchInfo.getShowState()) {
			// 正在进行的比赛
			case AttentionGameInfo.SHOW_STATE_RIGHT_NOW:
				childViewHolder.tvTimeState.setTextColor(resources
						.getColor(R.color.match_state_right_now));
				childViewHolder.tvHostScore
						.setBackgroundResource(R.drawable.img_default_score_ing);
				childViewHolder.tvGuestScore
						.setBackgroundResource(R.drawable.img_default_score_ing);
				childViewHolder.tvHostScore.setText(matchInfo.getHomeScore());
				childViewHolder.tvGuestScore.setText(matchInfo.getAwayScore());
				break;
			// 未开赛的比赛
			case AttentionGameInfo.SHOW_STATE_FUTURE:
				childViewHolder.tvTimeState.setTextColor(resources
						.getColor(R.color.match_state_future));
				childViewHolder.tvHostScore
						.setBackgroundResource(R.drawable.img_default_score_soon);
				childViewHolder.tvGuestScore
						.setBackgroundResource(R.drawable.img_default_score_soon);
				childViewHolder.tvHostScore.setText("");
				childViewHolder.tvGuestScore.setText("");
				break;
			// 已完场的比赛
			case AttentionGameInfo.SHOW_STATE_DONE:
				childViewHolder.tvTimeState.setTextColor(Color.GRAY);
				childViewHolder.tvHostScore
						.setBackgroundResource(R.drawable.img_default_score_end);
				childViewHolder.tvGuestScore
						.setBackgroundResource(R.drawable.img_default_score_end);
				childViewHolder.tvHostScore.setText(matchInfo.getHomeScore());
				childViewHolder.tvGuestScore.setText(matchInfo.getAwayScore());
				break;
			default:
				break;
			}

			childViewHolder.tvTimeState
					.setText(matchInfo.getStateDescription());
			childViewHolder.ivMark
					.setImageResource(R.drawable.s_ib_star_marked);
			childViewHolder.ivMark
					.setOnClickListener(new OnUnmarkClickListener(
							groupPosition, childPosition));

			childViewHolder.rlMatchContainer.setVisibility(View.VISIBLE);
			childViewHolder.rlEmptyContainer.setVisibility(View.GONE);
			childViewHolder.rlProgress.setVisibility(View.GONE);

		} else if (state == GroupState.empty) {// 没有可显示的比赛时
			convertView.setBackgroundColor(Color.TRANSPARENT);
			childViewHolder.rlMatchContainer.setVisibility(View.GONE);

			childViewHolder.rlEmptyContainer.setVisibility(View.VISIBLE);
			childViewHolder.rlProgress.setVisibility(View.GONE);
		} else {// loading界面
			convertView.setBackgroundColor(Color.TRANSPARENT);
			childViewHolder.rlMatchContainer.setVisibility(View.GONE);
			childViewHolder.rlEmptyContainer.setVisibility(View.GONE);
			childViewHolder.rlProgress.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

	@Override
	public int getChildSize(int groupPosition) {
		return mChildList.get(groupPosition).size();
	}

	public void setChildList(ArrayList<AttentionGameInfo> rightNowList,
			int position) {
		mChildList.get(position).clear();
		mChildList.get(position).addAll(rightNowList);
		setGroupState(rightNowList.size(), position);
	}

	@Override
	public AttentionGameInfo getChild(int groupPosition, int childPosition) {
		return mChildList.get(groupPosition).get(childPosition);
	}

	class ChildViewHolder {
		RelativeLayout rlMatchContainer;
		RelativeLayout rlEmptyContainer;
		RelativeLayout rlProgress;
		TextView tvLeagueName;
		TextView tvHostNumber;
		TextView tvHostName;
		TextView tvHostScore;
		TextView tvGuestNumber;
		TextView tvGuestName;
		TextView tvGuestScore;
		TextView tvTimeState;
		ImageButton ivMark;

	}

	class OnUnmarkClickListener implements OnClickListener {

		private int groupPosition;
		private int childPosition;

		public OnUnmarkClickListener(int groupPosition, int childPosition) {
			this.groupPosition = groupPosition;
			this.childPosition = childPosition;
		}

		@Override
		public void onClick(View v) {
			if (!TextUtils.isEmpty(UserInfo.getUserInfo().getCk())) {
				if (!cancelAttentionIsDoing) {
					cancelAttentionIsDoing = true;
					canceledGroupPosition = groupPosition;
					canceledChildPosition = childPosition;

					String matchId = mChildList.get(groupPosition)
							.get(childPosition).getMatchid();

					new CancelAttenMatch(mCancelAttentionHandler, matchId)
							.startCommunicate();

				}

			} else {
				Toast.makeText(context, "请先登录...", Toast.LENGTH_SHORT).show();
			}

		}

	}

	class CancelAttentionHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:
				// MatchListManager.getInstance()
				// .updateAttentionState(
				// mChildList.get(canceledGroupPosition)
				// .get(canceledChildPosition)
				// .getMatchid(), false);
				mChildList.get(canceledGroupPosition).remove(
						canceledChildPosition);
				if (mChildList.get(canceledGroupPosition).isEmpty()) {
					mGroupList.get(canceledGroupPosition).setState(
							GroupState.empty);
				}
				notifyDataSetChanged();

				break;
			case Config.COMMUNICATER_EXCEPTION:
			case Config.COMMUNICATER_FAIL:
			default:
				Toast.makeText(context, "取消关注失败！", Toast.LENGTH_SHORT).show();
				break;
			}
			cancelAttentionIsDoing = false;
		}

	}
}
